<?php
include_once('common/header.php');
$strRefer = isset($_SERVER['HTTP_REFERER']) ? $_SERVER['HTTP_REFERER'] : '';
if($AppUI->id <= 0 || $strRefer == '') {
	$arrJson 	= array('error'=>1,'msg'=>$arrError[3]);
	echo $objRequest->getParam('callback') . '(' . Zend_Json::encode($arrJson) . ')';
	exit();
}
$intAction 		= $objRequest->getParam('act',null);
$strContent 	= $objRequest->getParam('content',null);
$intBlogId 		= $objRequest->getParam('blogid',null);
$intBcommentId	= $objRequest->getParam('bcommentid',null);
$intError		= 0;
$strFullname	= '';
if($intAction == 0 || !isset($intAction)) {
	if(trim($strContent) == '' || !isset($strContent)) $intError = 1;
	elseif(strlen($strContent) < 2) $intError = 2;
}
if($intError == 0) {
	$objBlog 	= new Blog();
	$strMessage	= '';
	$intLike	= 0;
	if($intAction == 0 || !isset($intAction)) {
		$intOwnerId	= 0;
		$intError	= $objBlog->addComment($AppUI->id, $intBlogId, Vfriend_Function::stripTags($strContent,2),$intOwnerId);
		if($intError == 0) {
			Vfriend_Function::deleteCookie('bcmt' . $intBlogId);
			$strMessage	= $arrMessage[1];
		} else {
			$objVfriendUser = new Vfriend_User();
			$objUser		= $objVfriendUser->getFull($intOwnerId);
			$strFullname	= $objUser->fullnamevn;
			$intError  	   += 12;
		}
	}
	elseif(isset($intAction) && $intAction == 1) {
		$intError = $objBlog->deleteComment($AppUI->id, $intBlogId, $intBcommentId);
		$strMessage = $arrMessage[2];
		Vfriend_Function::deleteCookie('bcmt' . $intBlogId);
		if($intError > 0) $intError = 4;
	}
	elseif(isset($intAction) && $intAction == 2) {
		$intError 	= $objBlog->addLikeCmt($AppUI->id, $intBlogId, $intBcommentId, $intLike);
		$strMessage = $arrMessage[3];
		if($intError > 0) {
			if($intError == 3) $intError = 12;
			else $intError += 4;
		}
	}
	if($intError == 0) {
		include_once('blog/comment.php');
		$objView	= Zend_Registry::get('objView');
		$strData	= $objView->fetch('blog/box-comment.tpl');
		$arrJson	= array('error'=>0, 'data'=>$strData,'msg'=>$strMessage,'like'=>Vfriend_Function::numberFormat($intLike), 'total'=>Vfriend_Function::numberFormat($intTotal));
		echo $objRequest->getParam('callback') . '(' . Zend_Json::encode($arrJson) . ')';
		exit();
	}
}
if($intError > 0) {
	$strError	= $arrError[$intError];
	$strError	= str_replace('[strFullname]', $strFullname, $strError);
	$arrJson 	= array('error'=>$intError,'msg'=>$strError);
	echo $objRequest->getParam('callback') . '(' . Zend_Json::encode($arrJson) . ')';
	exit();
}
?>
